unit equalizer;
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
interface
uses
{$IFNDEF FPC}
  Windows, XpMan,
{$ELSE}
  //LCS, LCL,
{$ENDIF}
  Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls,adaptors, utils, PL_consts, trans_ru;

Type PtrackBar = ^TTrackbar;
type

  { TEq }

  TEq = class(TForm)
    FR_19904: TTrackBar;
    FR_1760: TTrackBar;
    FR_2488: TTrackBar;
    FR_4976: TTrackBar;
    FR_3520: TTrackBar;
    FR_311: TTrackBar;
    FR_440: TTrackBar;
    FR_7040: TTrackBar;
    FR_622: TTrackBar;
    FR_55: TTrackBar;
    //{$ENDIF}
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    FR_9952: TTrackBar;
    FR_880: TTrackBar;
    FR_1244: TTrackBar;
    FR_14080: TTrackBar;
    Label1: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label2: TLabel;
    Label20: TLabel;
    Label3: TLabel;
    FR_77: TTrackBar;
    Label4: TLabel;
    FR_110: TTrackBar;
    Label5: TLabel;
    FR_156: TTrackBar;
    Label6: TLabel;
    FR_220: TTrackBar;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Tm: TCheckBox;
    Button5: TButton;
    Button6: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FR_14080Change(Sender: TObject);
    procedure FR_19904Change(Sender: TObject);
    procedure FR_9952Change(Sender: TObject);
    procedure FR_1760Change(Sender: TObject);
    procedure FR_2488Change(Sender: TObject);
    procedure FR_311Change(Sender: TObject);
    procedure FR_3520Change(Sender: TObject);
    procedure FR_440Change(Sender: TObject);
    procedure FR_4976Change(Sender: TObject);
    procedure FR_55Change(Sender: TObject);
    procedure FR_220Change(Sender: TObject);
    procedure FR_110Change(Sender: TObject);
    procedure FR_622Change(Sender: TObject);
    procedure FR_1244Change(Sender: TObject);
    procedure FR_7040Change(Sender: TObject);
    procedure FR_880Change(Sender: TObject);
    procedure FR_77Change(Sender: TObject);
    procedure FR_156Change(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  private
    { Private declarations }
    _eqs: array[0..eq_maxcnt-1] of Ptrackbar;
    procedure fix_positions;
    function Find_Eqs(const name: string): Ptrackbar;
  public
    { Public declarations }
    procedure FRQ_loadfromFile(const filename: string);
  end;

var
  Eq: TEq;

implementation
{$IFNDEF FPC}
{$R *.dfm}
{$ELSE}
{$R *.lfm}
{$ENDIF}
uses Unit1;



function TEQ.Find_Eqs(const name: string): Ptrackbar;
var
  i: Byte;
begin
  i := 0;
  while (i < eq_maxcnt)and(_eqs[i].Name <> name) do
  begin
    inc(i);
  end;
  if (i = eq_maxcnt) then
    Find_EQS := nil
  else
    Find_EQS := _eqs[i];
end;

procedure TEQ.fix_positions;
var
  i: Byte;
  str: string;
begin
  for i := 0 to eq_maxcnt - 1 do
  begin
    str := _eqs[i]^.GetNamePath;
    str := copy(str, 4, length(str) - 3);
    adaptors.ENGINE_Eq_SETFREQ(PlMain.hstrm, Plmain._fxs[i], strtoint(str), _eqs[i]^.position);
  end;
end;

procedure TEq.Button4Click(Sender: TObject);
begin
  Button2Click(Sender);
  eq.Hide;
end;

procedure TEq.Button1Click(Sender: TObject);
begin
  FIX_Positions;
end;

procedure TEq.Button3Click(Sender: TObject);
begin
  Fix_Positions;
  eq.Hide;
end;

procedure TEq.Button5Click(Sender: TObject);
var
  i: Byte;
  fl: TstringList;
  str: string;
begin
  fl := Tstringlist.Create;
  for i := 0 to eq_maxcnt - 1 do
  begin
    str := _eqs[i]^.GetNamePath;
    str := copy(str, 4, length(str) - 3);
    fl.append(str + ' ' + inttostr(_eqs[i]^.Position)); 
  end;
  utils.Save_FIle(fl, pl_consts.filt_equalizer);
  fl.freeInstance;
end;

procedure TEq.FormCreate(Sender: TObject);
var
  i: Byte;
begin
  _eqs[0] := @FR_55;
  _eqs[1] := @FR_77;
  _eqs[2] := @FR_110;
  _eqs[3] := @FR_156;
  _eqs[4] := @FR_220;
  _eqs[5] := @FR_311;
  _eqs[6] := @FR_440;
  _eqs[7] := @FR_622;
  _eqs[8] := @FR_880;
  _eqs[9] := @FR_1244;
  _eqs[10] := @FR_1760;
  _eqs[11] := @FR_2488;
  _eqs[12] := @FR_3520;
  _eqs[13] := @FR_4976;
  _eqs[14] := @FR_7040;
  _eqs[15] := @FR_9952;
  _eqs[16] := @FR_14080;
  _eqs[17] := @FR_19904;
  for i := 0 to eq_maxcnt - 1 do
    _eqs[i].Tag := i;
end;

procedure TEq.Button2Click(Sender: TObject);
var
  i: Byte;
begin
  for i := 0 to eq_maxcnt - 1 do
    _eqs[i]^.Position := 0;
end;

procedure TEq.FR_14080Change(Sender: TObject);
begin
  if tm.checked then
     adaptors.ENGINE_Eq_SETFREQ(PlMain.Hstrm, PlMain._fxs[TAG], 14080, -FR_14080.position);
end;

procedure TEq.FR_19904Change(Sender: TObject);
begin
  if tm.checked then
     adaptors.ENGINE_Eq_SETFREQ(PlMain.Hstrm, PlMain._fxs[TAG], 19904, -FR_19904.position);
end;

procedure TEq.FR_9952Change(Sender: TObject);
begin
  if tm.checked then
     adaptors.ENGINE_Eq_SETFREQ(PlMain.Hstrm, PlMain._fxs[TAG], 9952, -FR_9952.position);
end;

procedure TEq.FR_1760Change(Sender: TObject);
begin
  if tm.checked then
     adaptors.ENGINE_Eq_SETFREQ(PlMain.Hstrm, PlMain._fxs[TAG], 1760, -FR_1760.position);
end;

procedure TEq.FR_2488Change(Sender: TObject);
begin
  if tm.checked then
     adaptors.ENGINE_Eq_SETFREQ(PlMain.Hstrm, PlMain._fxs[TAG], 2488, -FR_2488.position);
end;

procedure TEq.FR_311Change(Sender: TObject);
begin
  if tm.checked then
     adaptors.ENGINE_Eq_SETFREQ(PlMain.Hstrm, PlMain._fxs[TAG], 311, -FR_311.position);
end;

procedure TEq.FR_3520Change(Sender: TObject);
begin
  if tm.checked then
     adaptors.ENGINE_Eq_SETFREQ(PlMain.Hstrm, PlMain._fxs[TAG], 3520, -FR_3520.position);
end;

procedure TEq.FR_440Change(Sender: TObject);
begin
  if tm.checked then
     adaptors.ENGINE_Eq_SETFREQ(PlMain.Hstrm, PlMain._fxs[TAG], 440, -FR_440.position);
end;

procedure TEq.FR_4976Change(Sender: TObject);
begin
  if tm.checked then
     adaptors.ENGINE_Eq_SETFREQ(PlMain.Hstrm, PlMain._fxs[TAG], 4976, -FR_4976.position);
end;

procedure TEq.FR_55Change(Sender: TObject);
begin
  if tm.checked then
     adaptors.ENGINE_Eq_SETFREQ(PlMain.Hstrm, PlMain._fxs[Tag], 55, -FR_55.position);
end;

procedure TEq.FR_220Change(Sender: TObject);
begin
  if tm.checked then
     adaptors.ENGINE_Eq_SETFREQ(PlMain.Hstrm, PlMain._fxs[Tag], 220, -FR_220.position);
end;

procedure TEq.FR_110Change(Sender: TObject);
begin
  if tm.checked then
     adaptors.ENGINE_Eq_SETFREQ(PlMain.Hstrm, PlMain._fxs[TAG], 110, -FR_110.position);
end;

procedure TEq.FR_622Change(Sender: TObject);
begin
  if tm.checked then
     adaptors.ENGINE_Eq_SETFREQ(PlMain.Hstrm, PlMain._fxs[TAG], 622, -FR_622.position);
end;

procedure TEq.FR_1244Change(Sender: TObject);
begin
  if tm.checked then
     adaptors.ENGINE_Eq_SETFREQ(PlMain.Hstrm, PlMain._fxs[TAG], 1244, -FR_1244.position);
end;

procedure TEq.FR_7040Change(Sender: TObject);
begin
  if tm.checked then
     adaptors.ENGINE_Eq_SETFREQ(PlMain.Hstrm, PlMain._fxs[TAG], 7040, -FR_7040.position);
end;

procedure TEq.FR_880Change(Sender: TObject);
begin
  if tm.checked then
     adaptors.ENGINE_Eq_SETFREQ(PlMain.Hstrm, PlMain._fxs[TAG], 880, -FR_880.position);
end;

procedure TEq.FR_77Change(Sender: TObject);
begin
  if tm.checked then
     adaptors.ENGINE_Eq_SETFREQ(PlMain.Hstrm, PlMain._fxs[TAG], 77, -FR_77.position);
end;

procedure TEq.FR_156Change(Sender: TObject);
begin
  if tm.checked then
     adaptors.ENGINE_Eq_SETFREQ(PlMain.Hstrm, PlMain._fxs[TAG], 156, -FR_156.position);
end;
procedure TEq.FRQ_loadfromFile(const filename: string);
var
   compi_name, value: string;
  fl1: classes.TstringList;
  psl: Pstringlist;
  i: Byte;
  j: Cardinal;
  pnt: Ptrackbar;
begin
  if (not Fileexists(filename)) then exit;
  fl1 := TstringList.Create;
  fl1.LoadFromFile(filename);
  for i := 0 to fl1.count - 1 do
  begin
    j := 1;
    compi_name := '';
    while(fl1.strings[i][j] <> ' ')do
    begin
      compi_name := compi_name + fl1.strings[i][j];
      inc(j);
    end;
    compi_name := 'FR_' + compi_name;
    value := copy(fl1.strings[i], j + 1, length(fl1.strings[i]) - j);
    pnt := FInd_Eqs(compi_name);
    
    if (strtoint(value) > 20)or( strtoint(value) < -20)or(pnt = nil) then
    begin
      _Error(_TEFILE_INVALID_FILE + ' ' + filename);
      exit;
    end;
    pnt^.position := -strtoint(value);
  end;
  fl1.FreeInstance;
end;
procedure TEq.Button6Click(Sender: TObject);
begin
  FRQ_loadfromFile(Open_File(Pl_consts.filt_equalizer));
end;

end.